using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

using System.Runtime.InteropServices;
using Microsoft.CommerceServer.Interop;
using PIPECONFIGLib;

namespace PipeModifier
{
	/// <summary>
	/// Summary description for ChooseComp.
	/// </summary>
	public class ChooseComp : System.Windows.Forms.Form
	{
		private System.Windows.Forms.ComboBox comboBox1;
		private System.Windows.Forms.Label label1;

		private PIPECONFIGLib.PipeManagerClass pcfManager = null;
		private string objType = string.Empty;
		private string stageName = string.Empty;
		private string compGUID = string.Empty;
		private System.Windows.Forms.ListView listView1;
		private System.Windows.Forms.ColumnHeader columnHeader1;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Button button2;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		public ListViewItem itemResualts = null;

		public ChooseComp(PIPECONFIGLib.PipeManagerClass pcfFileManager, string guid, string stgname, string type)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			stageName = stgname;
			objType = type;
			pcfManager = pcfFileManager;
			compGUID = guid;
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.comboBox1 = new System.Windows.Forms.ComboBox();
			this.label1 = new System.Windows.Forms.Label();
			this.listView1 = new System.Windows.Forms.ListView();
			this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
			this.button1 = new System.Windows.Forms.Button();
			this.button2 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// comboBox1
			// 
			this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.comboBox1.Location = new System.Drawing.Point(8, 32);
			this.comboBox1.Name = "comboBox1";
			this.comboBox1.Size = new System.Drawing.Size(296, 21);
			this.comboBox1.TabIndex = 0;
			this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(8, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(296, 16);
			this.label1.TabIndex = 2;
			this.label1.Text = "Stages:";
			// 
			// listView1
			// 
			this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																						this.columnHeader1});
			this.listView1.Location = new System.Drawing.Point(8, 56);
			this.listView1.MultiSelect = false;
			this.listView1.Name = "listView1";
			this.listView1.Size = new System.Drawing.Size(296, 232);
			this.listView1.TabIndex = 4;
			this.listView1.View = System.Windows.Forms.View.Details;
			// 
			// columnHeader1
			// 
			this.columnHeader1.Text = "Components";
			this.columnHeader1.Width = 292;
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(144, 296);
			this.button1.Name = "button1";
			this.button1.TabIndex = 5;
			this.button1.Text = "OK";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(224, 296);
			this.button2.Name = "button2";
			this.button2.TabIndex = 6;
			this.button2.Text = "Cancel";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// ChooseComp
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(312, 327);
			this.Controls.Add(this.button2);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.listView1);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.comboBox1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "ChooseComp";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Choose a Component";
			this.Load += new System.EventHandler(this.ChooseComp_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void ChooseComp_Load(object sender, System.EventArgs e)
		{
			// load the combo box
			comboBox1.Items.Add(stageName);
			comboBox1.Items.Add("All");

			comboBox1.SelectedIndex = 0;

			//loadListView(string.Empty);
		}

		private void loadListView(string stageType)
		{
			listView1.Items.Clear();

			PIPECONFIGLib.ISimpleList list = null;

			if(stageType=="All")
			{
				// load all values
				list = pcfManager.GetComponentsByStageAffinity("{D2ACD8E0-43C5-11D0-B85D-00C04FD7A0FA}", 0);
			}
			else
			{
				list = pcfManager.GetComponentsByStageAffinity(compGUID, 0);
			}

			IEnumerator enumerator = list.GetEnumerator();

			while(enumerator.MoveNext())
			{
				ListViewItem item = listView1.Items.Add(((PIPECONFIGLib.IDictionary)enumerator.Current)["Name"].ToString());
				item.Tag = ((PIPECONFIGLib.IDictionary)enumerator.Current)["clsid"].ToString();
			}
		}

		private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e)
		{
			// get the right values
			loadListView(comboBox1.Text);
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.OK;
			itemResualts =  listView1.SelectedItems[0];
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			this.DialogResult = DialogResult.Cancel;
		}
	}
}
